/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lintulaskenta.rekisteri;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author amjaalto
 */
public class Rekisteri {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("LintulaskentaPU"); //"KonttiPU");

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /*
     * Testaukseen
     */
    public void setEmf(EntityManagerFactory emf2) {
        Rekisteri.emf = emf2;
    }

    public Object mergeEntity(Object e) {
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        Object t = em.merge(e);
        em.getTransaction().commit();
        return t;
    }

    public List getResultList(String query, Class luokka) {
        EntityManager em = getEntityManager();
        return (List) em.createQuery(query, luokka).getResultList();
    }

    public Object getEntity(Class entityName, long id) {
        EntityManager em = emf.createEntityManager();
        return em.find(entityName, id);
    }
}
